import dts from 'vite-plugin-dts'
import { defineConfig } from 'vite'

function formatEntryFileNames(
  path: string,
  name: string,
  extendName: 'mjs' | 'cjs'
) {
  if (name.startsWith('')) {
    return `${name.replace(path, '')}.${extendName}`
  }
  return `[name].${extendName}`
}

export default defineConfig({
  plugins: [
    dts({
      entryRoot: './packages/utils',
      outDir: ['./packages/utils/dist'],
      tsconfigPath: './tsconfig.json'
    })
  ],
  build: {
    outDir: '/',
    cssCodeSplit: true,
    rollupOptions: {
      preserveEntrySignatures: 'allow-extension',
      input: ['./packages/utils/index.ts'],
      output: [
        {
          format: 'es',
          entryFileNames: ({ name }) =>
            formatEntryFileNames('packages/utils/', name, 'mjs'),
          preserveModules: true,
          exports: 'named',
          dir: './packages/utils/dist'
        }
        // {
        //   format: 'es',
        //   entryFileNames: ({ name }) =>
        //     formatEntryFileNames('packages/hooks/', name, 'mjs'),
        //   preserveModules: true,
        //   exports: 'named',
        //   dir: './packages/hooks/dist'
        // }
      ]
    }
  }
})
